var token = localStorage.getItem("key")

function changePsdShow(val) {
	var p = ''
	var i = ''
	if(val == 'old'){
		p='oldPsd'
		i='opimg'
	}
	if(val == 'new'){
		p='newPsd'
		i='npimg'
	}
	if(val == 're'){
		p='rePsd'
		i='rpimg'
	}
	var psd = document.getElementById(p);
	var img = document.getElementById(i);
	if(psd.type == "password"){
		psd.type = "text"
		img.src="img/login_icon_visible@2x.png"
	}else{
		psd.type = "password"
		img.src="img/login_icon_invisible@2x.png"
	}
}

function verifyPsd(id) {
	var ele = document.getElementById(id);
	var p1 = /^[A-Za-z\d]{9,16}$/
    var me = true
    if (!p1.test(ele.value)) {
    	me=false
    }
    return me
}

function dbPsd() {
	if ($("#newPsd").val() != '' && $("#rePsd").val() != '') {
		if ($("#newPsd").val() != $("#rePsd").val()) {
			mui.toast('两次密码不一致')
		}
	}
}

function updPassword() {
	var oldPsd = document.getElementById('oldPsd');
	var newPsd = document.getElementById('newPsd');
	var conPsd = document.getElementById('rePsd');
	if (oldPsd.value == '') {
		mui.toast('请输入您的旧密码')
		return
	}
	if (newPsd.value == '') {
		mui.toast('请输入您的新密码')
		return
	} else {
		if (!verifyPsd('newPsd')) {
			mui.toast('请输入9-16位的字母或数字新密码')
			return
		}
	}
	if (conPsd.value == '') {
		mui.toast('请输入您的确认密码')
		return
	} else {
		if (!verifyPsd('rePsd')) {
			mui.toast('请输入9-16位的字母或数字确认密码')
			return
		}
	}
		
	if ($("#newPsd").val() == $("#rePsd").val()) {
		if ($("#newPsd").val() != $("#oldPsd").val()) {
			showDialog()
			$.ajax({
	            type: "POST",
	            url: url_prefix + "user/edit/updateUserPassword",
	            data: {
	            	request: JSON.stringify({
						oldPassword: $("#oldPsd").val(),
						newPassword: $("#newPsd").val(),
						token: token
					})   
	            },
	            dataType: "json",
	            success: function(data){
	            	//plus.nativeUI.toast(data.message, {verticalAlign: 'center'});
	            	
	            	if(data.code == '200'){
	            		mui.toast(data.message); 
	            		localStorage.setItem("LoginData",null)
	            		localStorage.setItem("key",null)
	            		window.setTimeout("window.location.href='fastLogin.html'",1000); 
	            	} else if (data.code == '300'){
						mui.toast("帐号在其他设备上登录")
						window.location.href="fastLogin.html"
					} else {
						mui.toast(data.message); 
					}
	            	dismissDialog()
	            }
	        });
		} else {
			mui.toast('新密码不能与旧密码相同');
		}
	} else {
		dbPsd()
	}
}